package com.yorick.scheduler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @program: Scheduler
 * @description: 定时器
 * @author: Yorick
 * @create: 15/4/2022
 **/
@Component
@Slf4j
public class Scheduler {
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");

    /**
     * @program: Sync Scheduler
     * @description: 同步定时调度任务，没有指定线程池，则使用SpringBoot内置的线程池
     * @author: Yorick
     * @create: 15/4/2022
     **/
    // @Scheduled(fixedRate = 1000, initialDelay = 1000)
    public void syncScheduler() {
        Thread mainThread = Thread.currentThread();
        log.info("定时任务开始1：当前线程：{},{}", mainThread.getName(), format.format(new Date()));
    }

    /**
     * @program: Async Scheduler
     * @description: 异步定时调度任务，指定线程池执行定时调度任务，只需在Async后面指定线程池名即可
     * @author: Yorick
     * @create: 15/4/2022
    **/
    @Async("yorickThreadPool")
   // @Scheduled(fixedRate = 1000, initialDelay = 1000)
    public void asyncScheduler() {
        Thread mainThread = Thread.currentThread();
        log.info("定时任务开始2：当前线程：{},{}", mainThread.getName(), format.format(new Date()));
    }

}
